package com.bc66.tiger.bizsys.menucategories.service.impl;

import java.util.Vector;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ui.ModelMap;

import com.bc66.tiger.bizsys.menucategories.dao.impl.MenuCatogoriesDaoImpl;
import com.bc66.tiger.bizsys.menucategories.dto.MenuCategoriesBeanDto;
import com.bc66.tiger.bizsys.menucategories.dto.MenuCategoriesBeanDtoUtil;
import com.bc66.tiger.bizsys.menucategories.model.MenuCategoriesBean;
import com.bc66.tiger.bizsys.menucategories.service.IMenuCategoriesService;
import com.bc66.tiger.framework.base.BaseManager;
import com.bc66.tiger.framework.base.EntityDao;
import com.bc66.tiger.framework.page.Page;
import com.bc66.tiger.framework.page.PageRequest;

public class MenuCategoriesServiceImpl extends BaseManager<MenuCategoriesBean, java.lang.Long> implements
		IMenuCategoriesService {

	@Autowired
	@Qualifier("menuCatogoriesDao")
	private MenuCatogoriesDaoImpl _menuCatogoriesDao;
	
	
	@Override
	public void findByPageRequest(PageRequest pr,ModelMap mmap) {
		// TODO Auto-generated method stub
		
		
		Page page=_menuCatogoriesDao.findByPageRequest(pr);
		
		mmap.put("iTotalRecords", page.getTotalCount());
		
		mmap.put("iTotalDisplayRecords", page.getTotalCount());
		
		mmap.put("aaData",this.indexConvertJson(page));
		
		//return 
	}
	@Override
	public void newMenuCategories(MenuCategoriesBeanDto mcbd,ModelMap mmap){
		// TODO Auto-generated method stub
	
		try {
			
			MenuCategoriesBean mb=MenuCategoriesBeanDtoUtil.convertToModel(mcbd);
			
			this.saveOrUpdate(mb);

			mmap.put("code", "0");
			
			mmap.put("info", "提交成功");
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
				mmap.put("code", "-1");
				
				mmap.put("info", e.getMessage());
			
		}
		
		
	}
	
	public void selectOne(Long id,ModelMap mmap) {
		
		try {
			
			mmap.put("entityBeans", MenuCategoriesBeanDtoUtil.convertToDto(this.getById(id)));
			
			mmap.put("code", "0");
			
		} catch (Exception e) {
			// TODO: handle exception
			
			e.printStackTrace();
			mmap.put("code", "-1");
			
			mmap.put("info", e.getMessage());
			
		}
		
	};
	
	@Override
	public void deleteOne(Long id,ModelMap mmap){
		
		try {
			
			_menuCatogoriesDao.deleteById(id);
			
			
			mmap.put("code", "0");
			
			mmap.put("info", "删除成功");
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
			mmap.put("code", "-1");
			
			mmap.put("info", e.getMessage());
			
		}
		
		
	}
	
	
	@Override
	public MenuCategoriesBean getById(Long id) {
		// TODO Auto-generated method stub
		return _menuCatogoriesDao.getById(id);
	}
	
	@Override
	protected EntityDao getEntityDao() {
		// TODO Auto-generated method stub
		
		return _menuCatogoriesDao;
	}

	@Override
	public void saveOrUpdate(MenuCategoriesBean entity) {
		// TODO Auto-generated method stub
		_menuCatogoriesDao.saveOrUpdate(entity);
		
	}

	private Vector<Vector<Object>> indexConvertJson(Page page){
		
		Vector<Vector<Object>> vector =new Vector<Vector<Object>>();
		
		Vector<Object> vetcorElement=null;
		
		for(int i=0;i<page.getResult().size();i++){
			
			vetcorElement=new Vector<Object>();
			
			vetcorElement.add(((MenuCategoriesBean)(page.getResult().get(i))).getId());
			
			vetcorElement.add(((MenuCategoriesBean)(page.getResult().get(i))).getMenuname());
			
			vetcorElement.add(((MenuCategoriesBean)(page.getResult().get(i))).getEgname());
			
			vetcorElement.add(((MenuCategoriesBean)(page.getResult().get(i))).getPinyin());
			
			vetcorElement.add(((MenuCategoriesBean)(page.getResult().get(i))).getDiscription());
			
			vetcorElement.add(MenuCategoriesBeanDto.sdf.format(((MenuCategoriesBean)(page.getResult().get(i))).getCreattime()));
			
			vetcorElement.add(((MenuCategoriesBean)(page.getResult().get(i))).getSort());
			
			vector.add(vetcorElement);
			
		}
		
		return vector;
		
	}
	

	

}
